package com.virus.mall.controller.admin;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.virus.mall.model.Result;
import com.virus.mall.model.User;
import com.virus.mall.service.UserService;
import com.virus.mall.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Classname UserServlet
 * @Description NULL
 * @Date 2021/4/15 23:34
 * @Created by virus
 */
@WebServlet("/api/admin/user/*")
public class UserServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    private ObjectMapper objectMapper = new ObjectMapper();

    {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replaceAll("/api/admin/user/", "");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replaceAll("/api/admin/user/", "");
        if (op.equals("allUser")) {
            allUser(request, response);
        } else if (op.startsWith("deleteUser")){
            deleteUser(request, response);
        } else if (op.startsWith("searchUser")) {
            searchUser(request, response);
        }
    }

    /**
     * 获取全部用户
     * @param request
     * @param response
     * @throws IOException
     */
    private void allUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<User> userList = userService.allUser();
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(userList)));
    }

    /**
     * 删除用户
     * @param request
     * @param response
     * @throws IOException
     */
    private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        boolean result = userService.deleteUser(id);
        if (result) {
            response.getWriter().println(objectMapper.writeValueAsString(Result.ok()));
        }else {
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("删除用户失败")));
        }
    }

    /**
     * 模糊查询用户
     * @param request
     * @param response
     * @throws IOException
     */
    private void searchUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String word = request.getParameter("word");
        List<User> userList = userService.searchUser(word);
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(userList)));
    }
}
